// 2023/3/23
// https://leetcode.cn/problems/top-k-frequent-elements/description/?envType=study-plan-v2&envId=top-100-liked
// 前k个高频元素

class Solution {
    public:
        struct Compare{
            bool operator()(pair<int, int>& p1, pair<int, int>& p2){
                return p1.second < p2.second;
            }
        };
    
        vector<int> topKFrequent(vector<int>& nums, int k) {
            unordered_map<int, int> mp;
            for(auto& e : nums) mp[e]++;
            priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> heap;
            for(auto& e : mp) heap.push(e);
    
            vector<int> ans;
            while(k--)
            {
                ans.push_back(heap.top().first);
                heap.pop();
            }
            return ans;
        }
    };